package com.inno.life.domain.utils;

import com.inno.life.domain.websocket.ZeyPlannerSocket;
import org.springframework.context.ApplicationContext;

/**
 * @author sean
 * Spring 上下文工具
 */
public class ApplicationContextUtil {
    private static ApplicationContext applicationContext;
    public static void setApplicationContext(ApplicationContext context){
        ApplicationContextUtil.applicationContext = context;
    }

    /**
     * Get a bean instance
     * @param clazz type
     * @return Instance object
     * @param <T> type
     */
    public static <T> T getBean(Class<T> clazz){
        return applicationContext.getBean(clazz);
    }

    /**
     * Get a bean instance by name and type
     * @param name bean name
     * @param clazz bean type
     * @return Instance object
     * @param <T> type
     */
    public static <T> T getBean(String name,Class<T> clazz){
        return applicationContext.getBean(name,clazz);
    }

    /**
     * Get bean instances based on name
     * @param name bean name
     * @return Instance object
     */
    public static Object getBean(String name){
        return applicationContext.getBean(name);
    }

    /**
     * 获取WebSocket
     * @return WebSocket实例
     */
    public static ZeyPlannerSocket getWebSocket(){
        return getBean(ZeyPlannerSocket.class);
    }
}
